'use strict';

const Controller = require('egg').Controller;

class LoginController extends Controller {
    async index() {


        await this.ctx.render('login.html');
    }
    async login() {
        const { app, ctx } = this;
        let postdata = this.ctx.request.body;

        let member = await this.ctx.service.member.findByName(postdata.name);
        if (!member) {
            return await this.ctx.render('usererror.html');
        }
        if (postdata.password != member.password) {
            return await this.ctx.render('usererror.html');
        }
        ctx.session.member = member;
        //this.ctx.render('admin.html');

        ctx.response.redirect('/admin');


    }
    async update(){
        const id = this.ctx.session.member.id;
        let postdata = this.ctx.request.body;
        postdata.id = id;
        console.log(postdata);
        const data = await this.ctx.service.member.update(postdata);
        this.ctx.session = null;
        console.log(data);
        await this.ctx.render('update.html');
       // this.ctx.response.redirect('/admin');
    }
    async logout(){
      
        this.ctx.session = null;
       this.ctx.response.redirect('/login');
    }

}

module.exports = LoginController;
